from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import render, redirect, HttpResponse
from .models import *
from django.core.paginator import Paginator


# Create your views here.
# 注册
def zhuce(request):
    if request.method == 'GET':
        return render(request, 'zhuce.html')
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        name = request.POST.get("name")
        User.objects.create_user(username=username, password=password)
        if name == '0':
            # return HttpResponse("学生")
            return redirect('sdenglu')
        elif name == '1':
            # return HttpResponse("老师")
            return redirect('tdenglu')


# 学生登录
def sdenglu(request):
    if request.method == 'GET':
        return render(request, 'sdenglu.html')
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        user = authenticate(username=username, password=password)
        if user:
            login(request, user=user)
            return redirect('addjia')
        return HttpResponse("登陆失败")


# 老师登录
def tdenglu(request):
    if request.method == 'GET':
        return render(request, 'tdenglu.html')
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        user = authenticate(username=username, password=password)
        if user:
            login(request, user=user)
            return redirect('shower')
        return HttpResponse("登陆失败")


# 添加假条
def addjia(request):
    if request.method == 'GET':
        t = QingJia.objects.all()
        return render(request, 'jiatiao.html', {'t': t})
    if request.method == 'POST':
        name = request.POST.get('name')
        reason = request.POST.get('reason')
        state = request.POST.get('state')
        stype = request.POST.get('stype')
        Student.objects.create(name=name, stype_id=stype, state=state, reason=reason)
        return HttpResponse('请假成功，请耐心等待')

# 添加类型
def addtiao(request):
    if request.method == 'GET':
        return render(request, 'addtiao.html')
    if request.method == 'POST':
        tname = request.POST.get("tname")
        t = QingJia(tname=tname)
        t.save()
        return HttpResponse("添加成功")


# 展示
@login_required
def shower(request):
    pag = Student.objects.all()
    page = request.GET.get("page")
    paginator = Paginator(pag, 3)
    now = paginator.get_page(page)
    last = now.number - 1
    next = now.number + 1
    max = paginator.num_pages
    small = 1
    if last == small:
        last = small
    if next == max:
        next = max
    return render(request, 'shower.html', context={
        'now': now, 'last': last, "next": next, 'max': max
    })


# 通过
def delsh(request, id):
    a = Student.objects.get(id=id)
    a.delete()
    return redirect('shower')


# 不通过
def hoot(request, id):
    if request.method == 'GET':
        t = QingJia.objects.all()
        return render(request, 'hoot.html', {"t": t})
    if request.method == 'post':
        name = request.POST.get("name")
        reason = request.POST.get("reason")
        state = request.POST.get("state")
        stype = request.POST.get("stype")
        s = Student.objects.get(id=id)
        s.name = name
        s.reason = reason
        s.state = state
        s.stype = stype
        s.stype_id = s.stype
        s.save()
        return redirect('shower')


def suo(request):
    if request.method == 'GET':
        s = QingJia.objects.all()
        return render(request, 'sou.html', {'s': s})
    if request.method == 'POST':
        stype = request.POST.get("tname")
        a = Student.objects.filter(stype_id=stype)
        return render(request, 'chu.html', {'a': a})
